package common

import (
	"exercise/application/yiran/app/libs"
	"exercise/application/yiran/app/request"
	"exercise/application/yiran/app/service"
	"github.com/gin-gonic/gin"
)

type uploaderController struct {
}

func NewUploader() *uploaderController {
	return &uploaderController{}
}

// UploadFile 文件上传
func (u *uploaderController) UploadFile(ctx *gin.Context) {
	uploaderFileRequest := new(request.UploadFileRequest)
	err := libs.BindRequest(ctx, uploaderFileRequest)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}

	fileId, err := service.NewUploaderService().UploadFile(ctx, uploaderFileRequest)
	if err != nil {
		libs.Toast(ctx, err.Error())
		return
	}
	libs.Success(ctx, fileId)
}

// SliceUpload 切片上传
func (u *uploaderController) SliceUpload(c *gin.Context) {
	request := request.NewSliceUploadFileRequest()
	if err := libs.BindRequest(c, request); err != nil {
		libs.Toast(c, err.Error())
		return
	}

	ret, err := service.NewUploaderService().SliceUpload(request)
	if err != nil {
		libs.Toast(c, err.Error())
		return
	}

	libs.Success(c, ret)
}

// MergeChunks 合并文件请求
func (u *uploaderController) MergeChunks(c *gin.Context) {
	request := request.NewSliceUploadFileRequest()
	if err := libs.BindRequest(c, request); err != nil {
		libs.Toast(c, err.Error())
		return
	}

	ret, err := service.NewUploaderService().MergeSliceChunks(request)
	if err != nil {
		libs.Toast(c, err.Error())
		return
	}

	libs.Success(c, ret)
}
